//
//  MedicalRecordPictureCell.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2021/8/19.
//  Copyright © 2021 QDSG. All rights reserved.
//

import UIKit

class MedicalRecordPictureCell: UITableViewCell {
    
    var imageClickedClosure: ((_ imageUrls: [String], _ index: Int) -> Void)?
    
    private var viewModel: MedicalRecordCellViewModel?
    
    func bind(to viewModel: MedicalRecordCellViewModel) {
        self.viewModel = viewModel
        
        collectionHeightConstraint.constant = viewModel.collectionViewHeight
    }
    
    @IBOutlet private var collectionHeightConstraint: NSLayoutConstraint!

    @IBOutlet private weak var collectionView: UICollectionView! {
        didSet {
            collectionView.isHidden = true
            collectionView.showsVerticalScrollIndicator = false
            collectionView.showsHorizontalScrollIndicator = false
            collectionView.delaysContentTouches = false
            collectionView.delegate = self
            collectionView.dataSource = self
            collectionView.register(MedicalRecordPictureItemCell.nibForCell(),
                                    forCellWithReuseIdentifier: MedicalRecordPictureItemCell.reuseIdentifier())
        }
    }
    
}

extension MedicalRecordPictureCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel?.imageUrls.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: MedicalRecordPictureItemCell.reuseIdentifier(), for: indexPath) as! MedicalRecordPictureItemCell
        if let viewModel = viewModel {
            cell.url = viewModel.imageUrls[indexPath.item]
        }
        return cell
    }
}

extension MedicalRecordPictureCell: UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        guard let viewModel = viewModel else { return .zero }
        
        return CGSize(width: viewModel.itemWH, height: viewModel.itemWH)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        guard let viewModel = viewModel else { return }
        
        imageClickedClosure?(viewModel.imageUrls, indexPath.item)
    }
    
    func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func collectionView(_ collectionView: UICollectionView, didHighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .splLineNormal
    }
    
    func collectionView(_ collectionView: UICollectionView, didUnhighlightItemAt indexPath: IndexPath) {
        let cell = collectionView.cellForItem(at: indexPath) as? PublishPhotoCell
        cell?.backgroundColor = .clear
    }
}
